package com.xzlzx.springbootgroovy.controller

import com.xzlzx.springbootgroovy.entity.Repo
import com.xzlzx.springbootgroovy.entity.TestBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

import javax.persistence.EntityManager

@RestController
class TestController {

//    @Autowired
//    Repo repo
//
//    @Autowired
//    EntityManager em

    @Autowired
    JdbcTemplate jdbcTemplate

    @Transactional
    @GetMapping("/api")
    String test(@RequestParam String param) {
        // query
//        def res = repo.findAll()
//        // join
//        def aa = repo.joinUseSQL(param)
//
//        // update，存在id就修改，不存在就新增
//        def tbUpdate = new TestBean()
//        tbUpdate.id = 2L
//        tbUpdate.name = param
//        repo.save(tbUpdate)
//
//        // insert
//        def tbInsert = new TestBean()
//        tbInsert.name = 'insert1'
//        repo.save(tbInsert)
//
//        // delete by id，不存在会抛错
//        if (repo.existsById(3L)) {
//            repo.deleteById(3L)
//        }
////
//        // delete by entity，不存在不会抛错，底层代码同上
//        def tbDelete = new TestBean()
//        tbDelete.id = 6L
//        repo.delete(tbDelete)

        // batch
//        def list = []
//        30.times {
//            def batchInsert = new TestBean()
//            batchInsert.id = it
//            batchInsert.name = "name$it"
//            list << batchInsert
//            em.merge(batchInsert)
//        }
//        em.flush()
//        em.clear()
//        repo.saveAll(list)


        return "123"
    }
}
